package drone.iqgc.common ;

import java.io.IOException ;

import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.ConversionNotSupportedException ;
import org.springframework.beans.TypeMismatchException ;
import org.springframework.http.converter.HttpMessageNotReadableException ;
import org.springframework.http.converter.HttpMessageNotWritableException ;
import org.springframework.web.HttpMediaTypeNotAcceptableException ;
import org.springframework.web.HttpRequestMethodNotSupportedException ;
import org.springframework.web.bind.MissingServletRequestParameterException ;
import org.springframework.web.bind.annotation.ExceptionHandler ;
import org.springframework.web.bind.annotation.RestControllerAdvice ;

import drone.basis.base.BaseResult ;


@RestControllerAdvice
public class ExceptionAdvice
{

	private static final Logger logger = LoggerFactory.getLogger ( ExceptionAdvice.class ) ;

	// 运行时异常
	@ExceptionHandler ( RuntimeException.class )
	public BaseResult < Object > runtimeExceptionHandler (
			RuntimeException ex
	)
	{
		ex.printStackTrace ( );
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 空指针异常
	@ExceptionHandler ( NullPointerException.class )
	public BaseResult < Object > nullPointerExceptionHandler (
			NullPointerException ex
	)
	{
		ex.printStackTrace ( );
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 类型转换异常
	@ExceptionHandler ( ClassCastException.class )
	public BaseResult < Object > classCastExceptionHandler (
			ClassCastException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// IO异常
	@ExceptionHandler ( IOException.class )
	public BaseResult < Object > iOExceptionHandler (
			IOException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 未知方法异常
	@ExceptionHandler ( NoSuchMethodException.class )
	public BaseResult < Object > noSuchMethodExceptionHandler (
			NoSuchMethodException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 数组越界异常
	@ExceptionHandler ( IndexOutOfBoundsException.class )
	public BaseResult < Object > indexOutOfBoundsExceptionHandler (
			IndexOutOfBoundsException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 400错误
	@ExceptionHandler ( HttpMessageNotReadableException.class )
	public BaseResult < Object > httpMessagetNotReadableExceptionHandler (
			HttpMessageNotReadableException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 400错误
	@ExceptionHandler ( TypeMismatchException.class )
	public BaseResult < Object > typeMismatchExceptionHandler (
			TypeMismatchException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 400错误
	@ExceptionHandler ( MissingServletRequestParameterException.class )
	public BaseResult < Object > missingServletRequestParameterExceptionHandler (
			MissingServletRequestParameterException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 405错误
	@ExceptionHandler ( HttpRequestMethodNotSupportedException.class )
	public BaseResult < Object > httpRequestMethodNotSupportedExceptionHandler (
			HttpRequestMethodNotSupportedException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 406错误
	@ExceptionHandler ( HttpMediaTypeNotAcceptableException.class )
	public BaseResult < Object > httpMediaTypeNotAcceptableExceptionHandler (
			HttpMediaTypeNotAcceptableException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	// 500错误
	@ExceptionHandler ( ConversionNotSupportedException.class )
	public BaseResult < Object > conversionNotSupportedExceptionHandler (
			ConversionNotSupportedException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}

	@ExceptionHandler ( HttpMessageNotWritableException.class )
	public BaseResult < Object > httpMessageNotWritableExceptionHandler (
			HttpMessageNotWritableException ex
	)
	{
		logger.info ( ex.getMessage ( ) ) ;
		return new BaseResult.Builder < Object > ( ).codeInfo ( "0" ).messageInfo ( ex.getMessage ( ) ).build ( ) ;
	}
}
